package com.staka.website.pages.user;

import java.util.Iterator;
import java.util.List;

import javax.persistence.Query;

import org.apache.tapestry5.alerts.AlertManager;
import org.apache.tapestry5.annotations.InjectComponent;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.corelib.components.Zone;
import org.apache.tapestry5.hibernate.annotations.CommitAfter;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.hibernate.Session;

import com.staka.website.entities.User;

public class Browse {
	@Inject
	private Session session;
	
	@InjectComponent
	private Zone zone;
	
	@Property
	private User user;
	
	@Inject
	private AlertManager alertmanager;
	
	public List<User> getUsers() {
		return session.createCriteria(User.class).list();
	}
	@CommitAfter
	public void onActionFromDelete(long id){
	    session.delete(session.get(User.class, id));
	}
	public int countUsers(){
		Query query = (Query) session.createQuery("select statement here");// do as you please with the query result

	    /**
	     * instead of doing a second query to get the count.  Just do a quick count using Iterator
	     */

	    int count = 0;
		for (Iterator iterator = ((org.hibernate.Query) query).iterate(); iterator.hasNext();) {
	        iterator.next();
	        count++;
	    }

	    Integer queryCount = count; //do as you please with the count
	    return queryCount;
	}
}
